package com.likianta.anykey;

import java.text.SimpleDateFormat;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.Date;

import butterknife.BindView;

/**
 * Created by Likianta_Dodoora on 2018/1/13 0013.
 */

public class Page2Child4 extends Fragment {
    
    private final int UUID = 0;
    private View view;
    
    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.page2_frag4, container, false);
        
        return view;
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        
        TextView info1 = (TextView) view.findViewById(R.id.ext_account_info);
        
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd    HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());
        String time = formatter.format(curDate);
        
        String staticStr1 = "创建时间：";
        String staticStr2 = "\n更新时间：";
        
        
        /*
         * 使用SpannableStringBuilder注意：
         * 1. 先添加文本，然后再setSpan，最后把它设置到TextView里面
         * 2. 给TextView设置文本的时候，不要直接把生字符串扔进去，比如这个错误的做法：
         *     textView.setText("Created Date: " + spanStr);
         *     即便你对spanStr里面的内容做了span处理，也会导致它和前面的“Created Date: ”一起被误识别为普通String
         *     所以不能这样加
         * 3. 另外一个类似的问题就是，如果给SpannableStringBuilder做类似的处理，也会被误识别为普通String：
         *     builder.append("Created Date: " + spanStr);
         *     textView.setText(builder); -> 识别失败
         * 4. 因此这里强烈建议按照第一条的做法，先添加好所有想添加的文本，再setSpan，最后再一口气扔进TextView里面：
         *     builder.append("Created Date: " + rawStr);
         *     builder.setSpan(...);
         *     textView.setText(builder); -> Good
         * 5. 当对同一spanStr设置多次时（特别是指设置同一颜色的时候），最后一次设置的会把前面的冲掉：
         *     spanStr.setSpan(colorBlue, 5, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
         *     spanStr.setSpan(colorBlue, 10, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
         *     spanStr.setSpan(colorRed, 15, 20, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
         *     即便它们设置的位置是不同的，第二行也会导致第一行设置的无效化
         *     不过第三行使用的是另一个前景色，所以不受前两行的任何影响
         */
        
        SpannableStringBuilder accountInfo = new SpannableStringBuilder();
        ForegroundColorSpan textAccent2Color = new ForegroundColorSpan(getResources().getColor(R.color.blaTextAccent2));
        ForegroundColorSpan textWeakColor = new ForegroundColorSpan(getResources().getColor(R.color.blaTextWeak));
        
        accountInfo.append("创建时间：" + time + "\n更新时间：" + time);
        LogUtil.d("p2cf4 accountInfo  = " + accountInfo);
        LogUtil.d("p2cf4 accountInfo.length  = " + accountInfo.length());
        
        accountInfo.setSpan(textAccent2Color, 5, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        accountInfo.setSpan(textAccent2Color, time.length()+11, time.length()+21, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //accountInfo.setSpan(textWeakColor, 19, 27, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //accountInfo.setSpan(textWeakColor, 48, accountInfo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        
        //SpannableString strTime = new SpannableString(time);
        //strTime.setSpan(textAccent2Color, 0, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //strTime.setSpan(textWeakColor, 10, strTime.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        
        // test
        //SpannableString testString = new SpannableString("Created Date: " + strTime + "\nUpdated Date: " + strTime);
        //testString.setSpan(textAccent2Color, 5, testString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //LogUtil.d("p2cf4 test spannable string is: " + testString);
        
        //int start = 5;
        //accountInfo.append("创建时间：" + time);
        //int end = accountInfo.length();
        //accountInfo.setSpan(textAccent2Color, 5, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //accountInfo.setSpan(textWeakColor, start + 10, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //
        //accountInfo.append("\n更新时间：" + time);
        //start = 5 + 7;
        //end = accountInfo.length();
        //accountInfo.setSpan(textAccent2Color, start, start + 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //accountInfo.setSpan(textWeakColor, start + 10, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        
        info1.setText(accountInfo);
        
        
    }
    
    
}
